package io.modifier.basic.emo.ui.core.ex

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.os.Build
import android.util.Size
import android.view.WindowManager

fun Context.findActivity(): Activity? {
    var context = this
    while (context is ContextWrapper) {
        if (context is Activity) return context
        context = context.baseContext
    }
    return null
}

fun Context.getWindowSize(): Size {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val windowMetrics = wm.currentWindowMetrics
        Size(windowMetrics.bounds.width(), windowMetrics.bounds.height())
    } else {
        val displayMetrics = resources.displayMetrics
        Size(displayMetrics.widthPixels, displayMetrics.heightPixels)
    }
}
